<?php
require_once dirname(__FILE__)."/../init.php";

include "header.php";
include "navigation.php";

?>
<br/>
<?php
$success = $_GET['success'];
$operation = $_GET['operation'];

$operationDone = null;

if ($operation == "add") {
    $operationDone = "saved";
} else if ($operation == "delete") {
    $operationDone = "deleted";
} else if ($operation == "send") {
    $operationDone = "sent";
}

if ($success != null && $success == "true") {
?>
<fb:success message="Your message has been <?php echo $operationDone ?>." />
<?php
}

$owner = ServiceFactory::getUserService()->getByUid($_uid);
$smsList = ServiceFactory::getSMSService()->getSMSByStatus(SMS::STATUS_DRAFT, $owner->getId());

?>

<style>
    .smsRecipient, .smsMessage, .smsDate, .smsOption { border-bottom: 1px solid #ddd; padding: 7px 3px; vertical-align: top; }
    .smsRecipient { width: 25%;}
    .smsMessage { width: 40%;}
    .smsDate { width: 15%;}
    .smsOption { width: 15%;}
    .contactItemMobileNumber {color: #999;}
</style>
<table>
    <tr>
        <th>Recipient</th>
        <th>Message</th>
        <th>Date</th>
        <th>&nbsp;</th>
    </tr>
<?php
foreach ($smsList as $sms) {
$recipientName = $sms->getRecipientName();
$mobileNumber = $sms->getRecipientMobileNumber();
?>
    <tr>
        <td class="smsRecipient">
        <?php
        if (empty($recipientName)){
            echo $mobileNumber;
        } else {
            ?>
            <?php echo $recipientName ?><span class="contactItemMobileNumber"><?php echo $mobileNumber ?></span>
            <?php
        }
        ?>
        </td>
        <td class="smsMessage">
            <?php echo $sms->getMessage(); ?>
        </td>
        <td class="smsDate"><?php $sms->getLastModifiedDate(); ?></td>
        <td class="smsOption">
            <a href="#">Edit</a> | 
            <a href="#" clicktoshowdialog="delete_dialog_<?php echo $sms->getId(); ?>">Delete</a> |
            <a href="#" clicktoshowdialog="send_dialog_<?php echo $sms->getId(); ?>">Send</a>
        </td>
    </tr>
<?php
}
?>
</table>
<?php
foreach ($smsList as $sms) {
?>
    <fb:dialog id="delete_dialog_<?php echo $sms->getId(); ?>" cancel_button="1">
      <fb:dialog-title>Delete message</fb:dialog-title>
      <fb:dialog-content>Are you sure you want to delete this message?
          <form method="post" name="delete_form_<?php echo $sms->getId(); ?>" id="delete_form_<?php echo $sms->getId(); ?>" action="index.php?_action=deleteMessage">
          <input type="hidden" name="smsId" value="<?php echo $sms->getId(); ?>">
          <input type="hidden" name="sourcePage" value="drafts">
      </form>
      </fb:dialog-content>
      <fb:dialog-button type="submit" value="Yes" form_id="delete_form_<?php echo $sms->getId(); ?>"/>
    </fb:dialog>
<?php
}

foreach ($smsList as $sms) {
?>
    <fb:dialog id="send_dialog_<?php echo $sms->getId(); ?>" cancel_button="1">
      <fb:dialog-title>Send message</fb:dialog-title>
      <fb:dialog-content>Are you sure you want to send this message?
          <form method="post" name="send_form_<?php echo $sms->getId(); ?>" id="send_form_<?php echo $sms->getId(); ?>" action="index.php?_action=sendMessage">
          <input type="hidden" name="smsId" value="<?php echo $sms->getId(); ?>">
          <input type="hidden" name="sourcePage" value="drafts">
      </form>
      </fb:dialog-content>
      <fb:dialog-button type="submit" value="Yes" form_id="send_form_<?php echo $sms->getId(); ?>"/>
    </fb:dialog>
<?php
}

?>